package softtech.apps.callblocker.widget;

import softtech.apps.callblocker.R;
import softtech.apps.callblocker.TextEnable;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class ProviderWidget extends AppWidgetProvider{

	Context mContext;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// initializing widget layout
				RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
						R.layout.widget_enable_disable);
				
				// register for button event
				remoteViews.setOnClickPendingIntent(R.id.ivWidget,
						buildButtonPendingIntent(context));
//				if(TextEnable.getEnableApp(mContext)==1){
//					ReceiveWidget.UseApp=1;
//					remoteViews.setInt(R.id.ivWidget, "getsetBackgroundResource", R.drawable.widgetenable);
//				}
//				else{
//					ReceiveWidget.UseApp=0;
//					remoteViews.setInt(R.id.ivWidget, "getsetBackgroundResource", R.drawable.widgetdisable);
//				}
				// request for widget update
				pushWidgetUpdate(context, remoteViews);
	}
	public static PendingIntent buildButtonPendingIntent(Context context){
		if(ReceiveWidget.UseApp==0)
		{
			ReceiveWidget.UseApp=1;
		}
		else{
			ReceiveWidget.UseApp=0;
		}
		Intent intent = new Intent();
		intent.setAction("softtech.apps.callblocker.widget.UPDATE_WIDGET");
		return PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
	}
	public static void pushWidgetUpdate(Context context, RemoteViews remoteViews) {
		ComponentName myWidget = new ComponentName(context,
				ProviderWidget.class);
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		manager.updateAppWidget(myWidget, remoteViews);
	}
}
